<template>
  <div class="login-container">
    <div class="login-box">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="手机号" prop="phone">
          <el-input type="text" v-model="ruleForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <particles></particles>
  </div>
</template>

<script>
// 登入
import { login } from "@/api";

import particles from "./particle";

export default {
  components: {
    particles
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validateUsername = (rule, value, callback) => {
      // console.log(rule)
      if (value == "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        phone: "",
        password: ""
      },
      rules: {
        //用于修改校验规则
        phone: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      //加载登录加载动画
      const loading = this.$loading({
        lock: true,
        text: "正在登陆",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });

      //在输入用户名密码的同时 会和validate相关联起来
      this.$refs[formName].validate(valid => {
        if (valid) {
          //登入逻辑写在此处
          let data = {
            phone: this.ruleForm.phone,
            password: this.ruleForm.password
          };
          login(data)
            .then(res => {
              console.log(res);
              if (res.data.code == 200) {
                //关闭动画
                loading.close();
                // 存储用户ID
                let userId = res.data.account.id;
                localStorage.setItem("userId", res.data.account.id);
                //登入成功 跳转主页
                this.$router.push("/Mine");
              } else {
                loading.close();
                this.$alert("提示", "账号密码错误", {
                  confirmButtonText: "确定",
                  callback: action => {
                    this.$message({
                      type: "info",
                      message: `action: ${action}`
                    });
                  }
                });
              }
            })
            .catch(err => {
              //出错了
              loading.close();
              alert("账号密码不正确！");
            });
        } else {
          alert("error submit!!");
          loading.close();
          return false;
        }
      });
    }
  }
};
</script>

<style>
.login-container {
  width: 1200px;
  height: 100%;
  margin: 0 auto;
  color: #333;
  overflow: hidden;
}
.login-box {
  width: 600px;
  height: 400px;
  margin: 190px auto;
}
/* .login-container .login-box #el-froms{
  width: 600px;
  height: 100%;
} */
.login-box .el-form-item__content .btn {
  width: 300px;
  height: 40px;
  margin-left: 100px;
  border-radius: 8px;
}
     

</style>